class SOCK_Connector {

public:
	SOCK_Connector () : handle_(INVALID_SOCKET) {}

	// Initialize a passive-mode acceptor socket
	SOCK_Connector (INET_Addr &addr) 
	{
		open(addr);	
	}

	// A second method to initialize a passive-mode
	// acceptor socket, analogously to the constructor
	void open (INET_Addr &sock_addr) 
	{	
		sock_addr_ = sock_addr;
		// Create a local endpoint of communication
		handle_ = socket (PF_INET, SOCK_STREAM, 0);
		// Associate address with endpoint
		bind (handle_, sock_addr_.addr(), sock_addr_.size());
	}

	int connectingNoblock(void)
	{
		u_long nNoBlock = 1;
		ioctlsocket(handle_, FIONBIO, &nNoBlock);
		int iResult = connect( handle_, sock_addr_.addr(), sock_addr_.size());
		if (iResult == SOCKET_ERROR) {
			if (WSAGetLastError() != WSAEWOULDBLOCK)
			{
				closesocket(handle_);
				handle_ = INVALID_SOCKET;
				return -1;
			}
		}
		return 0;
	}

	void connecting (SOCK_Stream &s)
	{
		// Connect to server.
		int iResult = connect( handle_, sock_addr_.addr(), sock_addr_.size());
		if (iResult == SOCKET_ERROR) {
			closesocket(handle_);
			handle_ = INVALID_SOCKET;
		}
		s.set_handle(handle_);
	}

	void close (void)
	{
		closesocket(handle_);
	}

	SOCKET get_handle () const 
	{ return handle_; }

private:
	SOCKET handle_; // Socket handle factory
	INET_Addr sock_addr_; // Address
};
